﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WellBuilt.Model;

namespace WellBuilt.Service
{
    public class ConcreteBeamService
    {
        internal static ConcreteBeamOut ConcreteBeamResult(ConcreteBeamIn concreteBeamIn)
        {
            double C3;
            if (concreteBeamIn.LoadType == "1个集中荷载")
            {
                C3 = 1;
            }
            else if (concreteBeamIn.LoadType == "2个集中荷载")
            {
                C3 = 2;
            }
            else
            {
                C3 = 3;
            }

            //1个集中荷载
            double J12 = 1.4;
            double D12 = 1.1;
            double B6 = concreteBeamIn.P1;
            double B11 = concreteBeamIn.A1;
            double J3 = concreteBeamIn.L0_1;
            double I6 = concreteBeamIn.HNT_Width;
            double J6 = concreteBeamIn.HNT_Height;
            double E9 = concreteBeamIn.HNT_Grade;
            //按xb计算单筋梁最大承载弯矩M1
            double J21 = E9 <= 50 ? 1 : 1 - (E9 - 50) / 5 * 0.01;
            double F9 = 0;
            double G9 = 0;
            switch (E9)
            {
                case 25:
                    F9 = 11.5;
                    G9 = 1.27;
                    break;
                case 30:
                    F9 = 14.3;
                    G9 = 1.43;
                    break;
                case 35:
                    F9 = 16.7;
                    G9 = 1.57;
                    break;
                case 40:
                    F9 = 19.1;
                    G9 = 1.71;
                    break;
                case 45:
                    F9 = 21.1;
                    G9 = 1.8;
                    break;
                case 50:
                    F9 = 23.1;
                    G9 = 1.89;
                    break;
                case 55:
                    F9 = 25.3;
                    G9 = 1.96;
                    break;
                case 60:
                    F9 = 27.5;
                    G9 = 2.04;
                    break;
            }
            double J19 = E9 <= 50 ? 0.8 : 0.8 - (E9 - 50) / 5 * 0.01;
            double E6 = concreteBeamIn.GJ_Marks;
            double F6 = 0;
            switch (E6)
            {
                case 300:
                    F6 = 270;
                    break;
                case 400:
                    F6 = 360;
                    break;
                case 500:
                    F6 = 435;
                    break;
            }
            double H6 = 200000;
            double H21 = 0.0033 - (E9 - 50) * Math.Pow(10, (-5));
            double J20 = H21 >= 0.0033 ? 0.0033 : H21;
            double I9 = concreteBeamIn.SLGJ_Layer;
            double J9 = I9 == 1 ? J6 - 35 : J6 - 60;
            double H19 = Math.Round(J19 / (1 + F6 / H6 / J20), 3);
            double H20 = Math.Round(H19 * J9, 1);
            double H22 = Math.Round(J21 * F9 * I6 * H20 * (J9 - H20 / 2) / 1000000, 2);
            double I10 = C3 == 1 ? Math.Round(D12 * J12 * B6 * B11 * (J3 - B11) / J3, 2) : 0;
            double C6 = concreteBeamIn.P2;
            double B9 = concreteBeamIn.A;
            double C9 = concreteBeamIn.B;
            double D9 = J3 - B9 - C9;
            double I11 = C3 == 2 ? Math.Round(D12 * J12 * C6 * B9 * (C9 + 2 * D9) / J3, 2) : 0;

            double H3 = concreteBeamIn.Q;
            double I12 = C3 == 3 ? D12 * J12 * H3 * Math.Pow(J3, 2) / 8 : 0;
            double H23 = Math.Round(24 * I6 * J6 * Math.Pow((1.05 * J3), 2) / 8 / 1000000 * D12 * J12, 2);
            double J23;
            switch (C3)
            {
                case 1:
                    J23 = Math.Round(I10 + H23 * J12 * D12, 2);
                    break;
                case 2:
                    J23 = Math.Round(I11 + H23 * J12 * D12, 2);
                    break;
                default:
                    J23 = Math.Round(I12 + H23 * J12 * D12, 2);
                    break;
            }

            double B15 = concreteBeamIn.SLGJ_Diameter;
            double G6 = F6;
            double E15 = concreteBeamIn.SYGJ_Diameter;

            double F17 = concreteBeamIn.GJ_Marks;
            double B17 = concreteBeamIn.GJ_Diameter;
            double C17 = concreteBeamIn.GJ_Number;

            double E17 = concreteBeamIn.GJ_Spacing;

            double H15 = concreteBeamIn.WQGJ_Diameter;
            double I15 = concreteBeamIn.WQGJ_Radical;
            double J15 = Math.Round(Math.Pow(H15, 2) * Math.PI / 4 * I15, 0);
            double I16 = concreteBeamIn.WQGJ_Angle;

            ConcreteBeamOut concreteBeamOut = new ConcreteBeamOut();

            concreteBeamOut.DJ = J23 < H22 ? "配单筋" : "配双筋";
            concreteBeamOut.DJ_Calculated = J23;
            concreteBeamOut.DJ_Admissible = H22;

            double J24 = 35;
            double H27 = (concreteBeamOut.DJ == "配双筋") ? Math.Round((J23 * 1000000 - J21 * F9 * I6 * H20 * (J9 - 0.5 * H20)) / G6 / (J9 - J24), 0) : 0;
            double F15 = (concreteBeamOut.DJ == "配双筋") ? (Math.Round(H27 / (Math.Pow(E15, 2) * Math.PI / 4) + 0.4, 0) <= 2 ? 2 : Math.Round(H27 / (Math.Pow(E15, 2) * Math.PI / 4) + 0.4, 0)) : 2;
            double G15 = Math.Round(Math.Pow(E15, 2) * Math.PI / 4 * F15, 0);
            double H26 = (concreteBeamOut.DJ == "配双筋") ? Math.Round((J21 * F9 * I6 * H20 + G15 * G6) / F6, 0) : H27;
            double H24 = (concreteBeamOut.DJ == "配单筋") ? Math.Round(J9 - Math.Pow((Math.Pow(J9, 2) - 2 * J23 * 1000000 / (J21 * F9 * I6)), 0.5), 1) : 0;
            double H25 = (concreteBeamOut.DJ == "配单筋") ? Math.Round(J21 * F9 * I6 * H24 / F6, 0) : 0;
            double C15 = (concreteBeamOut.DJ == "配双筋") ? Math.Round(H26 / (Math.Pow(B15, 2) * Math.PI / 4), 0) : Math.Round(H25 / (Math.Pow(B15, 2) * Math.PI / 4), 0);
            double D15 = Math.Round(Math.Pow(B15, 2) * Math.PI / 4 * C15, 0);
            double H28 = (concreteBeamOut.DJ == "配双筋") ? Math.Round((F6 * D15 - G6 * G15) / J21 / F9 / I6, 1) : 0;
            double J25 = (concreteBeamOut.DJ == "配双筋") ? H28 : H24;

            concreteBeamOut.SYQ_Height = J25 <= H20 ? (J25 > 2 * J24 ? "受压区高度x符合规定" : "受压区高度x偏小") : "受压区高度x偏大";
            concreteBeamOut.SYQ_Values = J25;
            concreteBeamOut.SYQ_Limit = H20;
            //=IF(H29>J23,"强度OK","重新配筋或选断面")
            double H29 = (concreteBeamOut.DJ == "配双筋") ? Math.Round(J21 * F9 * I6 * H28 * (J9 - 0.5 * H28) / 1000000 + G15 * G6 * (J9 - J24) / 1000000, 0) : Math.Round((J21 * F9 * I6 * H24 * (J9 - H24 / 2) + G15 * G6 * (J9 - J24)) / 1000000, 2);
            concreteBeamOut.LPJ_Result = H29 > J23 ? "梁配筋强度OK" : "重新配筋或选断面";
            concreteBeamOut.LPJ_Calculated = J23;
            concreteBeamOut.LPJ_Admissible = H29;

            double D17 = Math.Round(Math.Pow(B17, 2) * Math.PI / 4 * C17, 0);
            double H35 = Math.Round((0.7 * G9 * I6 * J9 + F17 * D17 / E17 * J9) / 1000, 0);
            double J35 = C3 == 1 ? Math.Round(B11 * B6 / J3 * 1.05 + 25 * I6 * J6 * J3 * 1.05 / 2 / 1000000, 2) : 0;
            double J36 = C3 == 2 ? Math.Round(1.05 * C6 * (2 * B9 + C9) / J3 + 25 * I6 * J6 * J3 * 1.05 / 2 / 1000000, 2) : 0;
            double J37 = C3 == 3 ? Math.Round(H3 * J3 / 2 * 1.05 + 25 * I6 * J6 * J3 * 1.05 / 2 / 1000000, 2) : 0;
            double H34 = C3 == 1 ? Math.Round(J35 * J12 * D12, 0) : (C3 == 2 ? Math.Round(J36 * J12 * D12, 0) : Math.Round(J37 * J12 * D12, 0));
            concreteBeamOut.WQGJ = H35 <= H34 ? "必须设弯起钢筋" : "可不设弯起钢筋";
            concreteBeamOut.WQGJ_Calculated = H34;
            concreteBeamOut.WQGJ_Admissible = H35;

            double H36 = H35 + Math.Round(0.8 * F6 * J15 * Math.Sin(I16 * Math.PI / 180) / 1000, 0);
            concreteBeamOut.KJ_Checking = H36 <= H34 ? "抗剪不够" : "抗剪验算OK";
            concreteBeamOut.KJ_Calculated = H34;
            concreteBeamOut.KJ_Admissible = H36;

            return concreteBeamOut;
        }
    }
}
